package fr.mywiki.business.xml;

import org.jdom.Element;

import fr.mywiki.business.utils.StringUtils;

/** Several methods to build standard html code. */
public class HtmlCodeBuilder {
	private HtmlCodeBuilder() {

	}

	/**
	 * Returns an anchor element having the specified name attribute, ie something
	 * of the form : <a name="..." href="...">...</a>
	 * 
	 * @param name
	 *          desired name of the anchor, name parameter
	 * @param inside
	 *          what is written inside the anchor, content of the anchor
	 * @return html code
	 */
	public static Element getAnchor(String name, String href, String inside) {
		Element elt = new Element("a");
		if (!StringUtils.isNullOrEmpty(name))
			elt.addAttribute("name", name);
		if (!StringUtils.isNullOrEmpty(inside))
			elt.addAttribute("href", href);
		if (!StringUtils.isNullOrEmpty(inside))
			elt.setText(inside);

		return elt;
	}

	/**
	 * Returns a span element, ie somthing of the form <span class="..."
	 * onclick="..." id="...">...</span>
	 * 
	 * @param classStr
	 *          class attribute
	 * @param onclick
	 *          onclick attribute (javascript code)
	 * @param id
	 *          id attribute
	 * @param inside
	 *          what is written inside the span, content of the span
	 * @return html code
	 */
	public static Element getSpan(String classStr, String onclick, String id,
			String inside) {
		Element elt = new Element("span");
		if (!StringUtils.isNullOrEmpty(classStr))
			elt.addAttribute("class", classStr);
		if (!StringUtils.isNullOrEmpty(onclick))
			elt.addAttribute("onclick", onclick);
		if (!StringUtils.isNullOrEmpty(id))
			elt.addAttribute("id", id);
		if (!StringUtils.isNullOrEmpty(inside))
			elt.setText(inside);
		return elt;
	}

	/**
	 * Returns a image element, ie somthing of the form <img src="..."
	 * border="..." width="..."/>
	 * 
	 * @param src
	 *          src attribute, source of the file
	 * @param border
	 *          border attribute
	 * @param width
	 *          width attribute
	 * @return html code
	 */
	public static Element getImage(String src, String border, String width) {
		Element elt = new Element("img");
		if (!StringUtils.isNullOrEmpty(src))
			elt.addAttribute("src", src);
		if (!StringUtils.isNullOrEmpty(border))
			elt.addAttribute("border", border);
		if (!StringUtils.isNullOrEmpty(width))
			elt.addAttribute("width", width);

		return elt;
	}

	/**
	 * Returns a <br/> element.
	 * 
	 * @return html code
	 */
	public static Element getBr() {
		return new Element("br");
	}
}
